﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Job.Logic.Company;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Web.Company
{
    public partial class CompanyRegister : System.Web.UI.Page
    {
        CompanyLogic logic = new CompanyLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //this.btCreate.Attributes.Add("onclick", "return enableSubmit();");
            }
            this.msg.Visible = false;
            this.lbErrMsg.Text = "";
        }

        protected void btCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.chkAllow.Checked)
                {
                    this.msg.Visible = true;
                    this.lbErrMsg.Text = "只有同意服务条款后，才能注册帐户！";
                    return;
                }
                JobCompanyInfo company = new JobCompanyInfo();
                company.LoginName = tbLogin.Text.Trim();
                company.LoginPswd = tbPassword1.Text;
                company.CompanyName = tbCompanyName.Text.Trim();
                company.CompanyCorporation = tbCorporation.Text.Trim();
                company.ContactPhone = tbPhone.Text.Trim();
                company.ContactMobile = tbMobile.Text.Trim();
                company.ContactEmail = tbEmail.Text.Trim();

                company.SecretSet = 0;
                company.AccountStatus = true;

                JobCompanyInfo info = logic.GetCompanyByLogin(company.LoginName);
                if (info != null)
                {
                    this.msg.Visible = true;
                    this.lbErrMsg.Text = "指定的登录名（" + company.LoginName + "）已被使用，请重新指定一个登录名！";
                }
                else
                {
                    bool isSucc = logic.Insert(company);
                    if (!isSucc)
                    {
                        this.msg.Visible = true;
                        this.lbErrMsg.Text = "系统内部故障，保存帐号信息失败！";
                    }
                    else
                    {
                        JobCompanyInfo dbCompany = logic.GetCompanyByLogin(company.LoginName);
                        if (null == dbCompany)
                        {
                            this.lbErrMsg.Text = "系统内部故障，保存帐号信息丢失！";
                            this.lbErrMsg.Visible = true;
                        }
                        else
                        {
                            MySession session = new MySession();
                            SessionCompany sCompany = new SessionCompany();
                            sCompany.CompanyId = dbCompany.CompanyInfoId;
                            sCompany.CompanyName = dbCompany.CompanyName;
                            session.AddCompany2Session(sCompany);

                            Response.Redirect("CompanyManager.aspx?categoryid=2&subjectid="
                                + ((int)Constants.CompanySubject.BaseInfo).ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.lbErrMsg.Text = "系统异常，无法创建账号！";
                this.lbErrMsg.Visible = true;
                this.msg.Visible = true;
            }
        }
    }
}
